<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <div class="filter-container">
      <el-select v-model="listQuery.post_state" placeholder="物流状态" clearable style="width: 140px" class="filter-item" >
        <el-option v-for="item in logisticsStatusOptions" :key="item.value" :label="item.name" :value="item.value" />
      </el-select>
      <el-date-picker v-model="listQuery.timeRange" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right"/>
      <el-input v-model="listQuery.search_num" placeholder="请输入订单编号或用户ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.goods_name" placeholder="请输入商品名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-checkbox v-model="listQuery.is_robot" :true-label="1" :false-label="0" class="filter-item" style="margin:0 15px;" >
        去除机器人数据
      </el-checkbox>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <!-- <el-button class="filter-item" type="success" icon="el-icon-document" @click="handleFilter">
        导出
      </el-button> -->
      <el-button class="filter-item" type="warning" size="mini" icon="el-icon-check" @click="batchDeal">
        标记为已处理
      </el-button>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="发起人" width="100">
        <template slot-scope="scope">
          <div class="user-box">
            <wish-image :src="scope.row.small_avatar_url" radius="100" width="45" height="45" />
            <p class="user-name">{{ scope.row.nick_name }}</p>
            <p class="user-id">{{ scope.row.user_id }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品名称/编号" min-width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.goods_name }}
        </template>
      </el-table-column>
      <el-table-column label="订单编号" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.group_id }}
        </template>
      </el-table-column>
      <el-table-column label="供货商" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.platform_name }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="收货信息" min-width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.receive_name }} {{ scope.row.receive_phone }}<br>
          {{ scope.row.address_info }}
        </template>
      </el-table-column>
      <el-table-column label="心愿达成时间" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.success_time }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="处理状态" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.post_state | statusFilter }}</span>
          <el-button v-if="scope.row.post_state===2" type="text" @click="cancelDeal(scope.row)">撤回</el-button>
        </template>
      </el-table-column>
      <el-table-column label="快递单状态" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.pageviews }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="140" align="center">
        <template slot-scope="{ row }">
          <el-button v-if="row.post_state>=1" type="primary" size="mini" @click="handleFill(row)">
            {{ row.post_order?'修改快递信息':'填写快递信息' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.page_size" @pagination="fetchData"/>
    <!--填写快递信息-->
    <el-dialog title="填写快递信息" :visible.sync="emsVisible" width="360px" :close-on-click-modal="false" @close="onEmsClose">
      <el-form ref="emsForm" :model="emsForm" :rules="emsFormRules" label-position="left" label-width="auto">
        <el-form-item label="快递公司" prop="post_company_name">
          <el-input v-model="emsForm.post_company_name"/>
        </el-form-item>
        <el-form-item label="快递单号" prop="post_order">
          <el-input v-model="emsForm.post_order"/>
        </el-form-item>
        <el-form-item label="发货时间" prop="post_time">
          <el-date-picker v-model="emsForm.post_time" style="width:100%" type="datetime" placeholder="选择发货时间"/>
        </el-form-item>
        <el-form-item label="快递备注" prop="remark">
          <el-input v-model="emsForm.remark"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="emsVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEms">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mergeObj, objRemove } from '@/utils'
import { getLogisticsList, fillLogistics, updateLogistics } from '@/api/logistics'
import WishImage from '@/components/Image'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { logisticsStatusOptions } from '@/constant/options'
export default {
  name: 'LogisticsManage',
  components: { WishImage, Pagination },
  filters: {
    statusFilter(value) {
      const status = logisticsStatusOptions.find(item => item.value === value)
      return status ? status.name : ''
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 15,
        post_state: -1,
        search_num: '',
        goods_name: '',
        is_robot: 0,
        timeRange: []
      },
      logisticsStatusOptions,
      total: 10,
      detailVisible: false,
      emsVisible: false,
      emsForm: {
        log_id: '',
        post_company_name: '',
        post_order: '',
        post_time: '',
        remark: ''
      },
      emsFormRules: {
        post_company_name: [{ required: true, message: '请输入快递公司', trigger: 'blur' }],
        post_order: [{ required: true, message: '请输入快递单号', trigger: 'blur' }]
      }

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const params = objRemove(this.listQuery)
      if (params.timeRange.length > 0) {
        params.start_time = params.timeRange[0]
        params.end_time = params.timeRange[1]
      }
      getLogisticsList(params).then(res => {
        this.list = res.data.data
        this.total = res.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    batchDeal() {
      this.$confirm('确认将搜索条件下的所有订单标记为已交由商家处理？（谨慎操作）', '', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const params = objRemove(this.listQuery)
        if (params.timeRange.length > 0) {
          params.start_time = params.timeRange[0]
          params.end_time = params.timeRange[1]
        }
        await updateLogistics(params)
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.fetchData()
      }).catch(() => {
      })
    },
    cancelDeal(row) {
      this.$confirm('确认撤销该订单已处理状态？', '', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

      }).catch(() => {
      })
    },
    handleFill(row) {
      this.emsForm = mergeObj(this.emsForm, row)
      this.emsVisible = true
    },
    saveEms() {
      this.$refs.emsForm.validate(async(valid) => {
        if (valid) {
          await fillLogistics(this.emsForm)
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.emsVisible = false
          this.fetchData()
        } else {
          console.log('valid fail')
        }
      })
    },
    onEmsClose() {
      this.$refs.detailForm.resetFields()
      this.emsForm = {
        log_id: '',
        post_company_name: '',
        post_order: '',
        post_time: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/ .detail-dialog{
  .el-dialog__body{
    padding: 10px 20px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
}

.dialog-content{
  display: flex;

}
.left-content{
  width: 180px;
  padding-right: 30px;
  border-right:1px solid #bbb;
  max-height: 400px;
  overflow: auto;
}
.right-content{
  flex:1;
  padding: 0px 43px 0px 63px;
  max-height: 400px;
  overflow: auto;
}
.user-box{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .user-name,.user-id{
    font-size: 14px;
    color:#6F6F6F;
  }
}
.formitem-inline{
  display: flex;
  .el-form-item{
    flex:1;
  }
  .el-form-item:not(:last-child){
    margin-right: 30px;
  }
}

.formitem-inline2{
  display: flex;
  .el-form-item{
    flex:1;
  }
  .el-form-item:not(:last-child){
    margin-right: 80px;
  }
  // .float-100{
  //   float: left;
  //   width: 100%;
  // }
}
</style>
